import time
from datetime import timedelta

from fastapi import Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from jose.exceptions import ExpiredSignatureError, JWTClaimsError

import bcrypt

from config import get_secret_key, get_secret_key_admin


SECRET_KEY = get_secret_key()
SECRET_KEY_ADMIN = get_secret_key_admin()
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="api/user/token")
credentials_exception = HTTPException(
    status_code=401,
    detail="验证用户失败，请重新登录",
    headers={"WWW-Authenticate": "Bearer"},
)
credentials_exception_admin = HTTPException(
    status_code=401,
    detail="验证管理员失败，请重新登录",
    headers={"WWW-Authenticate": "Bearer"},
)

def create_access_token(
    uid: str, expires_delta: timedelta = timedelta(hours=1), **extra_data
) -> str:
    to_encode = extra_data.copy()
    expire = int(time.time()) + expires_delta.total_seconds()

    to_encode.update({"uid": uid})
    to_encode.update({"exp": expire})

    encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)

    return encoded_jwt

def create_access_token_admin(
        uid: str, aid: int, expires_delta: timedelta = timedelta(hours=1), **extra_data
) -> str:
    to_encode = extra_data.copy()
    expire = int(time.time()) + expires_delta.total_seconds()

    to_encode.update({"uid": uid})
    to_encode.update({"aid": aid})
    to_encode.update({"exp": expire})

    print(to_encode)

    encoded_jwt = jwt.encode(to_encode, SECRET_KEY_ADMIN, algorithm=ALGORITHM)
    print(encoded_jwt)
    return encoded_jwt

async def get_current_user(token: str = Depends(oauth2_scheme)):
    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=ALGORITHM)
        uid = payload.get("uid")

        if not uid:
            raise credentials_exception

    except JWTError:
        raise credentials_exception

    return uid

async def get_current_admin(token: str = Depends(oauth2_scheme)):
    try:
        payload = jwt.decode(token, SECRET_KEY_ADMIN, algorithms=[ALGORITHM])
        
        uid: str = payload.get("uid")
        aid: int = payload.get("aid")
        # exp: datetime = datetime.fromtimestamp(payload.get("exp"))
        
        if not uid or not aid:
            raise credentials_exception_admin
        
    except JWTError:
        raise credentials_exception
    
    return aid

async def get_current_admin_uid(token: str = Depends(oauth2_scheme)):
    try:
        payload = jwt.decode(token, SECRET_KEY_ADMIN, algorithms=ALGORITHM)
        uid = payload.get("uid")
        aid = payload.get("aid")

        if not uid:
            raise credentials_exception

        if not aid:
            raise credentials_exception_admin

    except JWTError:
        raise credentials_exception

    return uid


async def login_required(token: str = Depends(oauth2_scheme)):
    try:
        jwt.decode(token, SECRET_KEY, algorithms=ALGORITHM)

    except JWTError:
        raise credentials_exception

    return True

async def login_required_admin(token: str = Depends(oauth2_scheme)):
    
    # return True

    try:
        jwt.decode(token, SECRET_KEY_ADMIN, algorithms=ALGORITHM)

    except JWTError:
        raise credentials_exception

    return True

def hash_passwd(passwd: str) -> str:
    pwd_bytes = passwd.encode("utf-8")
    salt = bcrypt.gensalt()
    hashed_password = bcrypt.hashpw(password=pwd_bytes, salt=salt)
    return hashed_password.decode('utf-8')


def verify_passwd(plain: str, hashed: bytes) -> bool:
    plain = plain.encode("utf-8")
    return bcrypt.checkpw(password=plain, hashed_password=hashed)
